﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HREDU;
using HREDU.Hoang_Tuan.Classes;

namespace HREDU.Hoang_Tuan
{
    public partial class frmQHGiaDinh : Form
    {
        public CDIADANH objDiaDanh;
        //public frmNHANSU frmParent;
        /// <summary>
        /// Id của nhân viên
        /// </summary>
        public int _ID_NHANVIEN;
        /// <summary>
        /// Cho biết có refresh lại datagrid hay không
        /// </summary>
        public bool _isRefresh = false;
        CCTQHGD objCTQHGD;
        public bool isBanThan;
        System.Int16 idQueQuan;
        Janus.Windows.GridEX.GridEX gridEX;

        public frmQHGiaDinh()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="frm"></param>
        /// <param name="isBanThan">Quan he gia dinh cua ban than hay ben vo (hoac chong)</param>
        public frmQHGiaDinh(int ID_NHANVIEN, bool _isBanThan, ref Janus.Windows.GridEX.GridEX _gridEX)
        {
            InitializeComponent();
            //frmParent = frm;
            _ID_NHANVIEN = ID_NHANVIEN;
            isBanThan = _isBanThan;
            gridEX = _gridEX;
        }

        public frmQHGiaDinh(int ID_NHANVIEN, int _id_ctqhgd, ref Janus.Windows.GridEX.GridEX _gridEX)
        {
            InitializeComponent();
            //frmParent = frm;
            _ID_NHANVIEN = ID_NHANVIEN;
            objCTQHGD = CCTQHGDCollection.LayObjectTheoID(_id_ctqhgd);
            isBanThan = objCTQHGD.THUOCBANTHAN;
            gridEX = _gridEX;
        }

        private void btnGhi_Click(object sender, EventArgs e)
        {
            //Kiem tra hop le
            if (txtHoTen.Text == "")
            {
                MessageBox.Show("Họ tên chưa có");
                txtHoTen.Focus();
                return;
            }

            //if (dtNgaySinh.Value == MyDate.DateNull())
            //{
            //    MessageBox.Show("Chưa chọn Ngày sinh");
            //    dtNgaySinh.Focus();
            //    return;
            //}

            //Gan du lieu
            objCTQHGD.ID_NHANVIEN = _ID_NHANVIEN;
            objCTQHGD.THUOCBANTHAN = isBanThan;
            int idQH = (int)cmbQuanHe.SelectedValue;
            objCTQHGD.ID_QHGD = (System.Int16)idQH;
            objCTQHGD.HOTEN = txtHoTen.Text;
            objCTQHGD.NAMSINH = txtNamSinh.Text;
            objCTQHGD.ID_QUEQUAN = idQueQuan;
            objCTQHGD.NOIO = txtNoiO.Text;
            objCTQHGD.NOIDUNG = txtNoiDung.Text;

            //Cap nhat
            if (objCTQHGD.ID_CTQHGD > 0)
            {
                if (CCTQHGDCollection.Update(objCTQHGD) > 0)
                    MessageBox.Show("Cập nhật thành công.");
            }
            else
            {
                if (CCTQHGDCollection.Insert(objCTQHGD) > 0)
                    MessageBox.Show("Thêm thành công.");
            }
            //Load lai du lieu
            //frmParent.LoadDataGridJanusQHGD(ref gridEX);
            _isRefresh = true;
           
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmQHGiaDinh_Load(object sender, EventArgs e)
        {
            //Mối quan hệ
            string strSQL = "select * from view_QuanHe";
            DataSet ds = MITI.BaseDB.Instance.ExecSql_DataSet(strSQL);
            cmbQuanHe.ValueMember = "ID";
            cmbQuanHe.DisplayMember = "TEN";
            cmbQuanHe.DataSource = ds.Tables[0].DefaultView;
            txtNamSinh.Text = "";
            if (objCTQHGD != null)
            {
                cmbQuanHe.SelectedValue = objCTQHGD.ID_QHGD;
                txtHoTen.Text = objCTQHGD.HOTEN;
                txtNamSinh.Text = objCTQHGD.NAMSINH;
                //Quê quán
                idQueQuan = objCTQHGD.ID_QUEQUAN;
                if (idQueQuan != -1)
                {
                    CDIADANH objDiaDanh = CDIADANHCollection.LayTheoID(idQueQuan);
                    TINHTHANH sQueQuan;
                    sQueQuan = objDiaDanh.TachTinhThanh();
                    txtTinh.Text = sQueQuan.Tinh;
                    txtHuyen.Text = sQueQuan.Huyen;
                    txtXa.Text = sQueQuan.Xa;
                }

                txtNoiO.Text = objCTQHGD.NOIO;
                txtNoiDung.Text = objCTQHGD.NOIDUNG;
            }
            else
            {
                objCTQHGD = new CCTQHGD();
                txtNamSinh.Text = "";
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmDIADANH frm = new frmDIADANH(this);
            frm.ShowDialog();
            if (objDiaDanh != null)
            {
                TINHTHANH sQueQuan;
                sQueQuan = objDiaDanh.TachTinhThanh();
                txtTinh.Text = sQueQuan.Tinh;
                txtHuyen.Text = sQueQuan.Huyen;
                txtXa.Text = sQueQuan.Xa;
                idQueQuan = (System.Int16)objDiaDanh.ID_DIADANH;
            }
        }

      
    }
}